#
# Find the CppUnit includes and library
#
# This module defines
# CPPUNIT_INCLUDE_DIR, where to find tiff.h, etc.
# CPPUNIT_LIBRARIES, the libraries to link against to use CppUnit.
# CPPUNIT_FOUND, If false, do not try to use CppUnit.

# also defined, but not for general use are
# CPPUNIT_LIBRARY, where to find the CppUnit library.
# CPPUNIT_DEBUG_LIBRARY, where to find the CppUnit library in debug mode.

option (CPPUNIT_USE_SHARED_LIBS "Build with cppunit shared or dynamic libraries" ON)

# User optional include dir
set (USER_CPPUNIT_INCLUDE_DIR CACHE PATH "CppUnit include path")
# User optional lib dir
set (USER_CPPUNIT_LIB_DIR CACHE PATH "CppUnit lib path")

FIND_PATH(CPPUNIT_INCLUDE_DIR cppunit/TestCase.h
  /usr/local/include
  /usr/include
  ${USER_CPPUNIT_INCLUDE_DIR})


# With Win32, important to have both
IF(WIN32)
  
  IF(CPPUNIT_USE_SHARED_LIBS)
    FIND_LIBRARY(CPPUNIT_LIBRARY cppunit_dll
                 ${CPPUNIT_INCLUDE_DIR}/../lib
		 ${USER_CPPUNIT_LIB_DIR})
    FIND_LIBRARY(CPPUNIT_DEBUG_LIBRARY cppunitd_dll
                 ${CPPUNIT_INCLUDE_DIR}/../lib
		 ${USER_CPPUNIT_LIB_DIR})
  ELSE(CPPUNIT_USE_SHARED_LIBS)
    FIND_LIBRARY(CPPUNIT_LIBRARY cppunit
                 ${CPPUNIT_INCLUDE_DIR}/../lib
		 ${USER_CPPUNIT_LIB_DIR})
    FIND_LIBRARY(CPPUNIT_DEBUG_LIBRARY cppunitd
                 ${CPPUNIT_INCLUDE_DIR}/../lib
		 ${USER_CPPUNIT_LIB_DIR})
  ENDIF(CPPUNIT_USE_SHARED_LIBS)
  
ELSE(WIN32)

  IF(CPPUNIT_USE_SHARED_LIBS)
  # On unix system, debug and release have the same name
    FIND_LIBRARY(CPPUNIT_LIBRARY libcppunit.so
               /usr/local/lib
               /usr/lib
               ${CPPUNIT_INCLUDE_DIR}/../lib	       
	       ${USER_CPPUNIT_LIB_DIR})
    FIND_LIBRARY(CPPUNIT_DEBUG_LIBRARY libcppunit.so
               /usr/local/lib
               /usr/lib
               ${CPPUNIT_INCLUDE_DIR}/../lib	       
	       ${USER_CPPUNIT_LIB_DIR})
  ELSE(CPPUNIT_USE_SHARED_LIBS)
    FIND_LIBRARY(CPPUNIT_LIBRARY libcppunit.a
               /usr/local/lib
               /usr/lib
               ${CPPUNIT_INCLUDE_DIR}/../lib	       
	       ${USER_CPPUNIT_LIB_DIR})
    FIND_LIBRARY(CPPUNIT_DEBUG_LIBRARY libcppunit.a
               /usr/local/lib
               /usr/lib
               ${CPPUNIT_INCLUDE_DIR}/../lib	       
	       ${USER_CPPUNIT_LIB_DIR})
  ENDIF(CPPUNIT_USE_SHARED_LIBS)
    
ENDIF(WIN32)

IF(CPPUNIT_INCLUDE_DIR)
  IF(CPPUNIT_LIBRARY)
    SET(CPPUNIT_FOUND "YES")
    SET(CPPUNIT_LIBRARIES ${CPPUNIT_LIBRARY})
    SET(CPPUNIT_DEBUG_LIBRARIES ${CPPUNIT_DEBUG_LIBRARY})
  ENDIF(CPPUNIT_LIBRARY)
ENDIF(CPPUNIT_INCLUDE_DIR)

IF (CPPUNIT_FOUND)
   # show which CppUnit was found only if not quiet
   IF (NOT CppUnit_FIND_QUIETLY)
      MESSAGE(STATUS "Found CppUnit: ${CPPUNIT_LIBRARY}")
   ENDIF (NOT CppUnit_FIND_QUIETLY)
ELSE (CPPUNIT_FOUND)
   # fatal error if CppUnit is required but not found
   IF (CppUnit_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find CppUnit, please use USER_CPPUNIT_INCLUDE_DIR or USER_CPPUNIT_LIB_DIR options or set your path environment variable correctly.")
   ENDIF (CppUnit_FIND_REQUIRED)
ENDIF (CPPUNIT_FOUND)
